package com.example.xoadocviet;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

	EditText et;
	Button bt_xoa, bt_doc, bt_viet;
	TextView tv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		et = (EditText) findViewById(R.id.editText1);
		bt_xoa = (Button) findViewById(R.id.button1);
		bt_doc = (Button) findViewById(R.id.button2);
		bt_viet = (Button) findViewById(R.id.button3);
		tv = (TextView) findViewById(R.id.textView1);

		bt_xoa.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				et.setText(" ");

			}
		});
		bt_doc.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try {
					// internal storage
					// FileInputStream fis = openFileInput("dulieu.txt");
					// external storage
					File duongdan = Environment.getExternalStorageDirectory();
					File taptin = new File(duongdan, "abc.txt");
					FileInputStream in = new FileInputStream(taptin);

					byte[] buffer = new byte[in.available()];
					in.read(buffer);
					et.setText(new String(buffer));
					in.close();

				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});
		bt_viet.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// internal storage
				try {
					// FileOutputStream fos = openFileOutput("dulieu.txt",
					// MODE_PRIVATE);

					// external storage vao cac thu muc public
					// File
					// duongdan=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

					// external storage
					// File duongdan=Environment.getExternalStorageDirectory();
					// File taptin=new File(duongdan, "abc.txt");

					String chuoiduongdan = Environment
							.getExternalStorageDirectory().toString();
					/*
					 * String s=""; for(int i=0;i<100;i++) s+="/a"+i;
					 */
					File duongdan = new File(chuoiduongdan + "/mot/hai/ba/bon");
					duongdan.mkdirs();
					File taptin = new File(duongdan, "bbb.txt");

					FileOutputStream out = new FileOutputStream(taptin);

					out.write(et.getText().toString().getBytes());
					out.close();

				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
